<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
include 'securities.php';
class Manage_user_products extends Securities {

	function __construct() {
		parent::__construct('manage_user_products');
		$this -> form_validation -> set_error_delimiters('<span class="error">', '</span>');
	}
	
	//Function to display product list of user post
	public function index()
	{
		$data['title']="Manage user products";
		
		$tbl_product=PREFIX_TABLE."products";
		$tbl_product_images=PREFIX_TABLE."product_images";
		$tbl_product_owner=PREFIX_TABLE."product_owners";
		
		$arr_fields=array("username,product_id,$tbl_product.name AS product_name,price,$tbl_product.status,$tbl_product.created_date,$tbl_product_images.name AS image_name");
		$arr_join=array($tbl_product_owner => array('use_product_owner_id' =>'product_owner_id'),$tbl_product_images=>array("product_id"=>"use_product_id"));
		
		$data['manage_datatable'] = get_user_products_manage_table($this -> mod_globals -> select_join_get_only_fields(PREFIX_TABLE."products", $arr_fields,$arr_join, $join_type = "INNER",  array($tbl_product_images.".priority"=>1),NULL,"product_id DESC"), $this -> uri -> segment(1));
		$this->load->view(B_MASTER.'index',$data);
	}
	//Function to remove product of user post
	public function delete_user_product($product_id){
		$module_id = $this -> uri -> segment(1);
		if (!$this -> is_has_modify_permission($module_id)) {
			redirect('ctm-common/no-allow-modify/' . $module_id);
		}

		$whe_delete_product = array('product_id' => $product_id);
		$is_deleted = $this -> mod_globals -> delete(PREFIX_TABLE . 'products', $whe_delete_product);
		if ($is_deleted) {
			$this -> session -> set_flashdata('success', 'Product data was deleted success.');
			redirect('manage-user-products');
		} else {
			$this -> session -> set_flashdata('error', 'Product data was deleted fail.');
			redirect('manage-user-products');
		}
	}
	
	
}
